package com.example.ivideo_2211a.fg

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseFragment
import com.example.ivideo_2211a.R
import com.example.ivideo_2211a.adapter.UserLivePreviewAdapter
import com.example.ivideo_2211a.databinding.FragmentZBBinding
import com.example.ivideo_2211a.home.HomeIntent
import com.example.ivideo_2211a.home.HomeViewModel
import com.example.ivideo_2211a.home.HomeViewState

class ZBFragment :  BaseFragment<FragmentZBBinding, HomeViewModel, HomeIntent, HomeViewState>() {

    private lateinit var userLivePreviewAdapter: UserLivePreviewAdapter

    override fun getLayoutId(): Int = R.layout.fragment_z_b

    override val viewModel: HomeViewModel by viewModels()

    override fun setupViews() {
        initViews()
        setupRecyclerViews()
    }

    override fun renderViewState(state: HomeViewState) {
        // 处理ViewState变化
    }

    private fun initViews() {
        // 初始化视图
        binding.apply {
            // 设置搜索框点击事件
            searchCardView.setOnClickListener {
                // TODO: 实现搜索功能
            }

            // 设置通知图标点击事件
            notificationIcon.setOnClickListener {
                // TODO: 实现通知功能
            }

            // 设置查看全部点击事件
            tvViewAll.setOnClickListener {
                // TODO: 实现查看全部分类功能
            }
        }
        binding.playZb.setOnClickListener {
            ARouter.getInstance().build("/app/StartLiveActivity").navigation()
        }
    }

    private fun setupRecyclerViews() {
        // 设置用户直播预览RecyclerView
        userLivePreviewAdapter = UserLivePreviewAdapter()
        binding.userLivePreviewRecyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = userLivePreviewAdapter
        }

        // 添加示例数据
        val userLivePreviewList = listOf(
            UserLivePreview("Laddy", R.drawable.bg_circle_red),
            UserLivePreview("Laddy", R.drawable.bg_circle_blue),
            UserLivePreview("Laddy", R.drawable.bg_circle_purple),
            UserLivePreview("Laddy", R.drawable.bg_circle_orange)
        )
        userLivePreviewAdapter.submitList(userLivePreviewList)
    }



//    override fun onIntent(intent: HomeIntent) {
//        when (intent) {
//            is HomeIntent.LoadData -> {
//                // TODO: 加载数据
//            }
//            else -> {
//                // 处理其他Intent
//            }
//        }
//    }

    // 用户直播预览数据类
    data class UserLivePreview(
        val username: String,
        val avatarBackground: Int
    )
}
